BM.control.zoom({position: 'bottomright'}).addTo(A.map);
A.popup = BM.popup({offset: BM.point(0, -30), minWidth: 400});
A.markers = {};
A.overlay = {};
A.icons = {};
A.selected = null;
A.nodeId = null;
A.folder = {};
A.treeSelected = {};
A.currentFolderId = null;
A.markerCluster = true;
A.locationMarker = BM.marker([0,0],{zIndexOffset:50000,icon: BM.divIcon({className: 'my-div-icon', html: '<div><span class="pulse-icon"></span></div>'})});
A.draw = {
    marker: new BM.Draw.Marker(A.map, {repeatMode: true}),
    polyline: new BM.Draw.Polyline(A.map, {repeatMode: true}),
    polygon: new BM.Draw.Polygon(A.map, {repeatMode: true}),
    circle: new BM.Draw.Circle(A.map, {repeatMode: true}),
    rectangle: new BM.Draw.Rectangle(A.map, {repeatMode: true}),
    measure: new BM.Draw.Polygon(A.map, {repeatMode: false}),
    ruler: BM.control.measure({}).addTo(A.map),
};
$('#jstree').on('contextmenu', function (e) {
    $('.context_menu').hide();
    let id = e.target.tagName == 'LI' ? e.target.id : e.target.tagName == 'UL'||e.target.tagName=='DIV' ? '0' : e.target.parentNode.id;
    let css_position = {left: e.clientX + 'px', top: e.clientY + 'px'};
    if (A.folder[id] || id === '0') {
        A.nodeId = id;
        if (id==='0'){
            $('.context_menu_add_folder').css(css_position).show();
        } else{
            $('.context_menu_add_parent').css(css_position).show();
        }
    } else if (A.markers[id]) {
        A.selected = A.markers[id];
        $('.context_menu_marker').css(css_position).show();
    } else if (A.overlay[id]) {
        A.selected = A.overlay[id];
        $('.context_menu_overlay').css(css_position).show();
    }
    return false;
}).on('click', function () {
    $('.context_menu').hide();
});

A.map.on('zoomend', function () {
    $('#map_status .current_zoom').html(this.getZoom());
    A.showMarkerInBonds();
});

A.map.on('mousemove', function (e) {
    $('#map_status .position').html(e.latlng.lng.toFixed(5) + ',' + e.latlng.lat.toFixed(5));
});

A.map.on('click', function (e) {
    if (A.selected && A.selected.is_editing && A.selected.editing.disable()) {
        //杩涜鏁版嵁鐨勬洿鏂版搷浣�
        let post_data = {overlay_id: A.selected.overlay_id, action_name: 'update_position'};
        switch (true) {
            case A.selected instanceof BM.Polygon:
                post_data.path = A.selected.getLatLngs();
                post_data.type = 'polygon';
                break;
            case A.selected instanceof BM.Polyline:
                post_data.path = A.selected.getLatLngs();
                post_data.type = 'polyline';
                break;
            case A.selected instanceof BM.Circle:
                post_data.path = [A.selected.getLatLng()];
                post_data.radius = A.selected.getRadius();
                post_data.type = 'circle';
                break;
            case A.selected instanceof BM.Rectangle:
                post_data.path = [A.selected.getLatLngs()[0], A.selected.getLatLngs()[2]];
                post_data.type = 'rectangle';
                break;
            default:
                return;
        }
        if (A.selected instanceof BM.Polygon){
            post_data.path = A.selected.getLatLngs();
            post_data.path=JSON.parse(JSON.stringify(post_data.path));
            for(let i=0;i<post_data.path.length;i++){
                post_data.path[i]=post_data.path[i].map(function (v) {
                    return [v.lat, v.lng];
                });
            }
        } else{
            post_data.path = post_data.path.map(function (v) {
                return [v.lat, v.lng];
            });
        }

        post_data.path=JSON.stringify(post_data.path);
        A.selected.is_editing = false;
        if (!A.login) return;
        $.ajax({
            type: 'post',
            url: '/map/admin/editOverlay.html',
            dataType: 'json',
            data: post_data,
            timeout: 5000,
            success: function () {

            }, error: function () {
                Tool.error('淇濆瓨鍑洪敊');
            }
        });
    }

    if (!$('#center_position').hasClass('hide')) {
        $('#center_position').val(e.latlng.lng + ',' + e.latlng.lat);
    }
    $('.context_menu').hide();
    $('.search_ul').hide();
});
A.map.on('moveend', function () {
    A.showMarkerInBonds();
});
A.map.on('zoomend', function () {
    A.showMarkerInBonds();
});
A.createOverlayId = function () {
    return new Date().getTime() * 10;
};

A.parseOption = function (data) {
    let option = {};
    data.color ? option.color = data.color : true;
    data.fill_color ? option.fillColor = data.fill_color : true;
    data.fill_opacity ? option.fillOpacity = data.fill_opacity : true;
    data.opacity ? option.opacity = data.opacity : true;
    data.weight ? option.weight = data.weight : true;
    return option;
};

A.showContextMenu = function (x, y, type) {
    $('.context_menu').hide();
    if (type == 'marker') {
        $('.context_menu_marker').css({left: x + 'px', top: y + 'px'}).show();
    } else {
        $('.context_menu_overlay').css({left: x + 'px', top: y + 'px'}).show();
    }
};

A.jstreeIcon = {
    marker: {
        title: '鏍囨敞',
        icon: '/static/map/images/tools/marker.png',
    },
    polygon: {
        title: '澶氳竟褰�',
        icon: '/static/map/images/tools/polygon.png'
    },

    polyline: {
        title: '绾挎',
        icon: '/static/map/images/tools/polyline.png',
    },

    rectangle: {
        title: '鐭╁舰',
        icon: '/static/map/images/tools/rectangle.png'
    },
    circle: {
        title: '鍦�',
        icon: '/static/map/images/tools/circle.png'
    },
    circlemarker: {
        title: '鍦�',
        icon: '/static/map/images/tools/circle.png'
    }
};

A.map.on(BM.Draw.Event.CREATED, function (e) {
    let type = e.layerType, layer = e.layer, post_data = {path: [], type: type}, overlay_id = A.createOverlayId(),
        overlay;
    switch (type) {
        case 'marker':
            post_data.path = [layer.getLatLng()];
            overlay = A.drawMarker({lat: layer.getLatLng().lat, lng: layer.getLatLng().lng, overlay_id: overlay_id});
            break;
        case 'polygon':
            if (A.drawType=='measure'){
                var area=BM.GeometryUtil.geodesicArea(layer.getLatLngs()[0]);
                area=area.toFixed(2).toString()+'銕�';
                layer.bindTooltip(area,{permanent:true}).on('contextmenu',function () {
                    this.remove();
                });
                layer.addTo(A.map);
                $('#draw_tool').find('a').removeClass('active').eq(0).addClass('active');
                return ;
            }
            overlay = A.drawOverlay({path: layer.getLatLngs(), overlay_id: overlay_id, type: type});
            post_data.path = layer.getLatLngs()[0];
            break;
        case 'polyline':
            overlay = A.drawOverlay({path: layer.getLatLngs(), overlay_id: overlay_id, type: type});
            post_data.path = layer.getLatLngs();
            break;
        case 'rectangle':
            overlay = A.drawOverlay({
                path: [layer.getLatLngs()[0], layer.getLatLngs()[2]],
                overlay_id: overlay_id,
                type: type
            });
            post_data.path = [layer.getLatLngs()[0][0], layer.getLatLngs()[0][2]];
            break;
        case 'circle':
        case'circlemarker':
            post_data.path = [layer.getLatLng()];
            post_data.radius = layer.getRadius();
            overlay = A.drawOverlay({
                path: layer.getLatLng(),
                radius: layer.getRadius(),
                overlay_id: overlay_id,
                type: type
            });
            break;
        default:
            return;
    }
    if (!overlay) {
        return false;
    }
    if (post_data.type == 'marker') {
        A.markers[overlay_id] = overlay;
    } else {
        A.overlay[overlay_id] = overlay;
    }
    A.treeSelected[overlay_id] = 1;
    A.jstree.bmTree('create_node', A.currentFolderId, {
        id: overlay_id,
        text: A.jstreeIcon[type]['title'],
        icon: A.jstreeIcon[type]['icon'],
    });
    A.currentFolderId ? post_data.pid = A.currentFolderId : 0;
    overlay.addTo(A.map);
    post_data.type = type;
    post_data.overlay_id = overlay_id;
    post_data.path = post_data.path.map(function (v) {
        return [v.lat, v.lng];
    });
    A.login && A.saveDraw(post_data);
}).on('stop_measure',function () {
    $('#draw_tool').find('a').removeClass('active').eq(0).addClass('active');
});
$('#draw_tool').on('click', 'a', function () {
    let action = $(this).data('action');
    $('#draw_tool').find('a').removeClass('active');
    $(this).addClass('active');
    for (let i in A.draw) {
        A.draw[i].disable();
    }
    switch (action) {
        case 'marker':
        case 'polyline':
        case 'polygon':
        case 'circle':
        case 'rectangle':
        case 'measure':
        case 'ruler':
            A.drawType=action;
            A.draw[action].enable();
            break;
    }
});
A.drawMarker = function (data) {
    let marker = BM.marker([data.lat, data.lng]);
    if (data.icon) {
        let i;
        typeof A.icons[data.icon] !== 'undefined' ? (i = A.icons[data.icon]) && marker.setIcon(BM.icon({
            iconUrl: data.icon,
            iconSize: [i.width, i.height],
            iconAnchor: [Math.floor(i.width / 2), i.height]
        })) : marker.setIcon(BM.icon({iconUrl: data.icon, iconSize: [30, 41], iconAnchor: [20, 41]}));
    }
    data.title && marker.bindTooltip(data.title, {direction: 'bottom', permanent: true});
    marker.overlay_id = data.overlay_id;
    marker.on('click', function () {
        A.selected = this;
        $.ajax({
            type: 'post',
            url: '/map/index/getPopup.html',
            data: {overlay_id: this.overlay_id, action_name: 'show_data'},
            timeout: 3000,
            success: function (data) {
                let y = -30;
                try {
                    y = A.selected.options.icon.options.iconSize['1'];
                    A.popup.options.offset.y = -y + 10;
                } catch (e) {
                    con(e);
                }
                A.popup.setLatLng(A.selected.getLatLng()).setContent(data.html).openOn(A.map);
            },
            error: function () {

            }
        });
    });
    marker.on('contextmenu', function (e) {
        A.showContextMenu(e.containerPoint.x, e.containerPoint.y, 'marker');
        A.selected = this;
    });
    return marker;
};

A.drawOverlay = function (data) {
    let overlay, option = A.parseOption(data);
    switch (data.type) {
        case 'polyline':
            overlay = BM.polyline(data.path, option);
            break;
        case 'polygon':
            overlay = BM.polygon(data.path, option);
            break;
        case 'rectangle':
            overlay = BM.rectangle(data.path, option);
            break;
        case 'circle':
            option.radius = data.radius || data.path.radius;
            overlay = BM.circle(data.path.center || data.path, option);
            break;
        case 'circle_marker':
            overlay = BM.circleMarker(data.path, {radius: data.radius});
            break;
        default:
            return false;
    }
    data.title && overlay.bindTooltip(data.title, {direction: 'bottom'});
    overlay.overlay_id = data.overlay_id;
    overlay.on('contextmenu', function (e) {
        A.selected = this;
        A.showContextMenu(e.containerPoint.x, e.containerPoint.y, 'overlay');
    });
    return overlay;
};

A.map.on('contextmenu', function () {

});

A.saveDraw = function (post_data) {
    $.ajax({
        type: 'post',
        url: '/map/admin/addOverlay.html',
        timeout: 3000,
        data: post_data,
        dataType: 'json',
        success: function (data) {

        },
        error: function () {

        }
    });
};

A.showMarkerInBonds = function () {
    let map_bounds = A.map.getBounds(), zoom = A.map.getZoom();
    A.grid = [];
    for (let k in A.markers) {
        if (map_bounds.contains(A.markers[k].getLatLng()) && A.treeSelected[A.markers[k].overlay_id]) {
            if (A.markerCluster) {
                if (!A.isInGrid(A.markers[k].getLatLng())||zoom>17) {
                    !A.markers[k]['_map'] ? A.markers[k].addTo(A.map) : true;
                } else {
                    A.markers[k].remove();
                }
            } else {
                !A.markers[k]['_map'] ? A.markers[k].addTo(A.map) : true;
            }
        } else {
            A.markers[k].remove();
        }
    }
};
A.showSelectedOverlay = function () {
    for (let k in A.overlay) {
        if (A.treeSelected[A.overlay[k].overlay_id]) {
            !A.overlay[k]['_map'] ? A.overlay[k].addTo(A.map) : true;
        } else {
            A.overlay[k].remove();
        }
    }
};

A.isInGrid = function (position) {
    for (let i = 0; i < A.grid.length; i++) {
        if (A.grid[i].contains(position)) {
            return true;
        }
    }

    let zoom = A.map.getZoom();
    A.grid.push(position.toBounds(40076000 / Math.pow(2, zoom) / 256 * 200));
    return false;
};
A.getNextOverlay=function(){
    $.ajax({
        type:'post',
        url:'/map/index/getMore.html',
        data:{
            action_name:'get_overlay',
            next_id:A.next_overlay_id
        },
        dataType:"json",
        timeout:6000,
        success:function (data) {
            if (data.next_id){
                A.next_overlay_id=data.next_id;
                setTimeout(function () {
                    A.getNextOverlay();
                },500);
            }
            for (let i in data.overlay) {
                data.overlay[i].path = JSON.parse(data.overlay[i].path);
                let o = A.drawOverlay(data.overlay[i]);
                o.addTo(A.map);
                let icon = A.jstreeIcon[data.overlay[i]['type']];
                A.jstree.bmTree('create_node',data.overlay[i]['pid'],{
                    id: o.overlay_id,
                    pid: data.overlay[i]['pid'],
                    icon: icon['icon'],
                    text: data.overlay[i]['title'] || icon['title']
                });
                A.treeSelected[o.overlay_id] = 1;
                A.overlay[o.overlay_id] = o;
            }
        },
        error:function () {
            A.getNextOverlay();
        }
    });
}
$.ajax({
    type: 'post',
    url: "/map/index/getData.html",
    dataType: 'json',
    timeout: 60000,
    success: function (data) {
        $('#jstree_loading').hide();
        A.icons = data.icons;
        let sum = 0;
        let jstree_data = [];
        for (let i in data.folder) {
            A.folder[data.folder[i]['overlay_id']] = '1';
            jstree_data.push({
                text: data.folder[i]['title'],
                pid: data.folder[i]['pid'],
                id: data.folder[i]['overlay_id']
            });
        }
        for (let i in data.marker) {
            let m = A.drawMarker(data.marker[i]);
            sum++;
            jstree_data.push({
                id: m.overlay_id,
                pid: data.marker[i]['pid'],
                icon: A.jstreeIcon['marker']['icon'],
                text: data.marker[i]['title'] ? data.marker[i]['title'] : A.jstreeIcon['marker']['title']
            });
            A.treeSelected[m.overlay_id] = 1;
            A.markers[m.overlay_id] = m;
        }
        A.showMarkerInBonds();
        for (let i in data.overlay) {
            data.overlay[i].path = JSON.parse(data.overlay[i].path);
            let o = A.drawOverlay(data.overlay[i]);
            o.addTo(A.map);
            let icon = A.jstreeIcon[data.overlay[i]['type']];
            jstree_data.push({
                id: o.overlay_id,
                pid: data.overlay[i]['pid'],
                icon: icon['icon'],
                text: data.overlay[i]['title'] || icon['title']
            });
            A.treeSelected[o.overlay_id] = 1;
            A.overlay[o.overlay_id] = o;
        }
        jstree_data = A.loop(jstree_data, 0);
        A.jstree = $('#jstree').bmTree({data: jstree_data}).on('drag_end', function (e, drag) {
            if (!A.login) return;
            Tool.post('', {action_name: 'update_parent', pid: drag.pid, id: drag.id}, function () {

            });
        }).on('before_drop', function (e, data) {
            return !!A.folder[data.pid];
        }).on('clicked.bm_tree', function (e, click_data) {
            let id = click_data.id;
            if (A.folder[id]) {
                A.currentFolderId = id;
            }
            if (click_data.selected.length <= 1) {
                A.markers[id] && A.map.setView(A.markers[id].getLatLng());
                A.overlay[id] && A.map.fitBounds(A.overlay[id].getBounds());
            } else {
                let bound = BM.latLngBounds();
                for (let i = 0; i < click_data.selected.length; i++) {
                    let t = click_data.selected[i];
                    A.markers[t] && bound.extend(A.markers[t].getLatLng());
                    A.overlay[t] && bound.extend(A.overlay[t].getBounds());
                }
                bound.getNorthEast() && A.map.fitBounds(bound);
            }
        }).on('select_node.bm_tree', function (e, select) {
            let t;
            for (let i = 0; i < select.select.length; i++) {
                t = select.select[i];
                if (!A.treeSelected[t]) {
                    A.treeSelected[t] = 1;
                }
            }
            A.showMarkerInBonds();
            A.showSelectedOverlay();
        }).on('deselect_node.bm_tree', function (e, deselect) {
            let t;
            for (let i = 0; i < deselect.deselect.length; i++) {
                t = deselect.deselect[i];
                if (A.treeSelected[t]) {
                    delete A.treeSelected[t];
                    A.markers[t] && A.markers[t].remove();
                    A.overlay[t] && A.overlay[t].remove();
                }
            }
        });
        A.getNextOverlay();
    },
    error: function () {
        $('#jstree_loading').find('.title').html('<span class="text-danger">鍔犺浇鍑洪敊锛岃鍒锋柊椤甸潰!</span>');
    }
});

$('#search_data').on('input', function () {
    let timer = $(this).data('timer');
    clearTimeout(timer);
    let val = $(this).val();
    if (val === '') {
        $('#search_ul').html('');
        return;
    }
    timer = setTimeout(function () {
        let i = 0, result = [], tooltip, content, position;
        for (let k in A.markers) {
            tooltip = A.markers[k].getTooltip();
            if (tooltip && i < 9 && (content = tooltip.getContent()) && content.indexOf(val) > -1) {
                i++;
                position = A.markers[k].getLatLng();
                result.push('<li><a href="javascript:void(0)"  data-lat="' + position.lat + '" data-lng="' + position.lng + '" data-operation="select_marker">' + content + '</a></li>');
            }
            if (i > 9) {
                break;
            }
        }
        for (let k in A.overlay) {
            tooltip = A.overlay[k].getTooltip();
            if (tooltip && i < 9 && (content = tooltip.getContent()) && content.indexOf(val) > -1) {
                i++;
                position = A.overlay[k].getBounds().getCenter();
                result.push('<li><a href="javascript:void(0)" data-lat="' + position.lat + '" data-lng="' + position.lng + '" data-operation="select_marker">' + content + '</a></li>');
            }
            if (i > 9) {
                break;
            }
        }
        if (!result.length) result.push('<li>娌℃湁缁撴灉</li>');
        $('#search_ul').html(result.join(''));
    }, 200);
    $(this).data('timer', timer);
});

A.loop = function (data, pid) {
    let loop_data = [];
    for (let i in data) {
        if (data[i]['pid'] == pid) {
            data[i]['children'] = A.loop(data, data[i]['id']);
            loop_data.push(data[i]);
        }
    }
    return loop_data;
}
$('[data-toggle="tooltip"]').tooltip();
$('#top_input').on('input', function () {
    let timer = $(this).data('timer');
    clearTimeout(timer);
    let val = $(this).val();
    timer = setTimeout(function () {
        let center = A.map.getCenter();
        $.get('https://restapi.amap.com/v3/assistant/inputtips?output=json&location=' + ([center.lng, center.lat].join(',')) + '&keywords=' + val + '&key=f290e69e458e618aead61401ee06b37e', function (data) {
            let result = [], item;
            if (data.tips) {
                for (let i = 0; i < data.tips.length; i++) {
                    item = data.tips[i];
                    typeof item.location == 'string' && item.location ? result.push('<li><a  data-title="'+item.name+'" data-operation="location" data-location="' + item.location + '" href="javascript:void(0);"><i class="fa fa-map-marker"></i>&nbsp; ' + item.name + '</a></li>') : true;
                }
            }
            if (!result.length) result.push('<li>娌℃湁缁撴灉</li>');
            $('.search_ul').html(result.join('')).show();
        });
    }, 300);
    $(this).data('timer', timer);
});


$('body').on('before_submit', '[data-form]', function (e, submit_data) {
    if ($(this).data('login')) {
        Tool.error('璇风偣鍑诲彸涓婅鐧婚檰!');
        submit_data._next = false;
        return;
    }
    switch (Tool.action_name) {
        case 'edit_marker':
            submit_data.data.overlay_id = A.selected.overlay_id;
            break;
        case 'change_icon':
            $('#inner_form input[name="data[icon]"]').val($(this).data('src'));
            $('#inner_form .current_icon').prop('src', $(this).data('src'));
            submit_data._next = false;
            break;
        case 'upload_marker':
            $(this).parent().find('input[type=file]').click();
            submit_data._next = false;
            break;
        case 'delete_img':
            $(this).parent().remove();
            submit_data._next = false;
            break;
        case 'change_map_type':
            submit_data._next = false;
            if (A.crs.name != $(this).data('projection')) {
                if (location.pathname.indexOf('admin')>-1){
                    window.location.href = '/map/admin.html?layer_id=' + $(this).data('index');
                }else{
                    window.location.href = '/map.html?layer_id=' + $(this).data('index');
                }
                submit_data._next=false;
                return;
            }
            A.currentLayer.remove();
            A.currentLayer = BM.tileLayer($(this).data('id'));
            A.currentLayer.addTo(A.map);
            submit_data._next=false;
            break;
        case 'add_parent':
            submit_data.data = {action_name: 'add_parent', pid: A.nodeId};
            Tool.action_name = 'save_add_parent';
            break;
        case 'save_setting':
            Tool.submit('/map/admin/setting.html', $('#setting_form').serialize(), function (data) {
                Tool.success('淇濆瓨鎴愬姛!', '');
            });
            submit_data._next = false;
            break;
        case 'edit_overlay':
            submit_data.data.overlay_id = A.selected.overlay_id;
            break;
        case 'edit_path':
            !A.selected.is_editing && A.selected.editing.enable() && (A.selected.is_editing = true);
            submit_data._next = false;
            break;
        case 'edit_parent':
            Tool.action_name = 'save_edit_parent';
            submit_data.data = {id: A.nodeId};
            break;
    }
}).on('after_submit', '[data-form]', function (e, data) {
    switch (Tool.action_name) {
        case 'edit_marker':
        case 'change_icon':
        case 'upload_marker':
            let marker_data = data.data;
            marker_data.icon && A.selected.setIcon(BM.icon({
                iconUrl: marker_data.icon,
                iconSize: [marker_data.width, marker_data.height],
                iconAnchor: [Math.floor(marker_data.width / 2), marker_data.height]
            }));
            typeof A.selected.getTooltip() === 'undefined' && marker_data.title ? A.selected.bindTooltip(marker_data.title, {
                direction: 'bottom',
                permanent: true
            }) : A.selected.setTooltipContent(marker_data.title);

            A.jstree.bmTree('set_text', A.selected.overlay_id, marker_data.title);
            data._next = false;
            break;
        case 'save_setting':
            window.location.reload();
            data._next = false;
            break;
        case 'edit_overlay':
            let option = A.parseOption(data.data);
            A.selected.setStyle(option);
            typeof A.selected.getTooltip() === 'undefined' && data.data.title ? A.selected.bindTooltip(data.data.title, {
                direction: 'bottom',
            }) : A.selected.setTooltipContent(data.data.title);
            A.jstree.bmTree('set_text', A.selected.overlay_id, data.data.title);
            data._next = false;
            break;
        case 'save_add_parent':
            let folder_data = data.data.folder;
            A.jstree.bmTree('create_node', folder_data.pid, {id: folder_data.overlay_id, text: folder_data.title});
            A.currentFolderId=folder_data.overlay_id;
            A.folder[folder_data.overlay_id] = 1;
            data._next = false;
            break;
        case 'save_edit_parent':
            A.jstree.bmTree('set_text', data.data.overlay_id, data.data.title);
            data._next = false;
            break;
    }
}).on('after_edit', '[data-form]', function () {
    switch (Tool.action_name) {
        case 'edit_overlay':
            $('#inner_form').find('[data-colors]').colorpicker();
            break;
    }
}).on('before_delete', '[data-form]', function (e, delete_data) {
    switch ($(this).data('delete')) {
        case 'marker':
        case 'overlay':
            delete_data.data = {id: A.selected.overlay_id};
            break;
        case 'folder_parent':
            delete_data.data = {id: A.nodeId};
            break;
    }
}).on('after_delete', '[data-form]', function (e, delete_data) {
    switch ($(this).data('delete')) {
        case 'marker':
            A.selected.remove();
            delete A.markers[A.selected.overlay_id];
            A.jstree.bmTree('delete_node', A.selected.overlay_id);
            delete_data._next = false;
            break;
        case 'overlay':
            A.selected.remove();
            delete A.overlay[A.selected.overlay_id];
            A.jstree.bmTree('delete_node', A.selected.overlay_id);
            delete_data._next = false;
            break;
        case 'folder_parent':
            let son=A.jstree.bmTree('get_son',delete_data.id);
            for (let i=0;i<son.length;i++){
                A.markers[son[i]]&&A.markers[son[i]].remove()&&delete A.markers[son[i]];
                A.overlay[son[i]]&&A.overlay[son[i]].remove()&&delete A.overlay[son[i]];
            }
            A.jstree.bmTree('delete_node', delete_data.id);
            delete_data._next = false;
            break;
    }
}).on('click', '[data-form]', function () {
    $('.context_menu').hide();
}).on('click', '[data-operation]', function () {
    $('.context_menu').hide();
    switch ($(this).data('operation')) {
        case 'export_kml':
            var son=A.jstree.bmTree('get_son',A.nodeId);
            var group=BM.featureGroup();
            for (let i=0;i<son.length;i++){
                var t=A.markers[son[i]];
                if (t){
                    t.getTooltip()&&(t.feature={type:'Feature',properties:{name:t.getTooltip().getContent()}});
                    group.addLayer(t);
                }
                t=A.overlay[son[i]];
                if (t){
                    t.getTooltip()&&(t.feature={type:'Feature',properties:{name:t.getTooltip().getContent()}});
                    group.addLayer(t);
                }
            }

            var blob=new Blob([tokml(group.toGeoJSON())]);
            var href=URL.createObjectURL(blob);
            $('#download_kml').prop('href',href);
            document.getElementById('download_kml').click();
            break;
        case 'edit_center':
            $('.center_setting').hide();
            $('#center_position').removeClass('hide');
            $(this).parent().hide();
            break;
        case 'location':
            let location = $(this).data('location');
            location = location.split(',');
            $('.search_ul').hide();
            if (location.length == 2) {
                location = [parseFloat(location['1']), parseFloat(location['0'])];
                if (!A.locationMarker) {
                    A.locationMarker = BM.marker(location).bindTooltip($(this).data('title'),{direction: 'bottom', permanent: true}).addTo(A.map);
                } else {

                    A.locationMarker.setLatLng(location).setTooltipContent($(this).data('title'));
                    !A.locationMarker['_map']&&A.locationMarker.addTo(A.map);
                }
                A.map.setView(location);
            }
            break;
        case 'lat_lng_location':
            let lat_lng=[];
            $(this).parent().find('input').each(function () {
                let v=$(this).val();
                if (!isNaN(parseFloat(v))) lat_lng.push(parseFloat(v));
            });
            if (lat_lng.length!=2)return;
            if (Math.abs(lat_lng[0])>180||Math.abs(lat_lng[1])>90)return;
            $.get('http://restapi.amap.com/v3/geocode/regeo?key=f290e69e458e618aead61401ee06b37e&location='+lat_lng.join(',')+'&poitype=&radius=&extensions=base&batch=false&roadlevel=0', function (data) {
                if (data.regeocode&&data.regeocode.formatted_address){
                    let str=typeof  data.regeocode.formatted_address =='string'?data.regeocode.formatted_address:' ';
                    if (!A.locationMarker) {
                        A.locationMarker=BM.marker([lat_lng['1'],lat_lng['0']]).bindTooltip(str,{direction: 'bottom', permanent: true}).addTo(A.map)
                    }else{
                        A.locationMarker.setLatLng([lat_lng['1'],lat_lng['0']]);

                        A.locationMarker.getTooltip()&&A.locationMarker.setTooltipContent(str);
                    };
                    !A.locationMarker['_map']&&A.locationMarker.addTo(A.map);
                    A.map.setView([lat_lng['1'],lat_lng['0']]);
                }
            });
            break;
        case 'cluster_marker':
            if ($(this).hasClass('btn-primary')) {
                $(this).addClass('btn-default').removeClass('btn-primary');
                A.markerCluster = false;
            } else {
                $(this).addClass('btn-primary').removeClass('btn-default');
                A.markerCluster = true;
            }
            break;
        case 'toggle_side_bar':
            if ($('#left_sidebar').width() > 50) {
                $('#left_sidebar').width(41).find('.toggle_div').hide();
                $(this).find('i').removeClass('fa-angle-double-left').addClass('fa-angle-double-right');
                $(this).parent().find('.title').hide();
            } else {
                $('#left_sidebar').width(300).find('.toggle_div').show();
                $(this).find('i').removeClass('fa-angle-double-right').addClass('fa-angle-double-left');
                $(this).parent().find('.title').show();
            }
            break;
        case 'select_marker':
            $('#search_ul').html('');
            let lat = $(this).data('lat'), lng = $(this).data('lng');
            A.map.flyTo([parseFloat(lat), parseFloat(lng)]);
            A.locationMarker.setLatLng([parseFloat(lat), parseFloat(lng)]);
            !A.locationMarker['_map']&&A.locationMarker.addTo(A.map);
            break;
        case 'move_marker':
            if (!A.selected || !A.login) return;
            A.selected.dragging.enable();
            A.selected.once('dragend', function () {
                this.dragging.disable();
                $.ajax({
                    type: 'post',
                    url: '/map/admin/editMarker.html',
                    data: {
                        overlay_id: this.overlay_id,
                        action_name: "update_position",
                        lng: this.getLatLng().lng,
                        lat: this.getLatLng().lat
                    },
                    timeout: 3000,
                    dataType: 'json',
                    success: function () {

                    },
                });
            });
            break;
    }
}).on('change', '[data-upload]', function () {
    switch (true) {
        case 'upload_marker' == Tool.action_name:
            let form_data = new FormData();
            let files = this.files, extension, upload_files = [];
            for (let i = 0; i < files.length; i++) {
                extension = files[i].name.split('.').pop().toLowerCase();
                if (['png', 'gif', 'jpg', 'jpeg'].indexOf(extension) === -1) continue;
                upload_files.push(files[i]);
            }

            if (!upload_files.length) return;
            for (let i = 0; i < upload_files.length; i++) {
                form_data.append('files[]', upload_files[i]);
            }
            form_data.append('action_name', 'upload_img');
            Tool.upload('/map/admin/editMarker.html', form_data, function (data) {
                $('#inner_form .marker_img_body').prepend(data.html);
            });
            break;
        case 'change_zoom' == $(this).data('action'):
            A.map.setZoom($(this).val());
            break;
    }
});